package com.soujiayi.store;

import com.soujiayi.util.Constants;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

	private static final String CREATE_SEARCHKEYHISTORY_SQL = "create table "
			+ Constants.SEARCHKEYHISTORY_TABLE_NAME
			+ "(_id integer PRIMARY KEY autoincrement, searchkey varchar(20),type varchar(2), timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)";

	private static final String CREATE_USERINFO_SQL = "create table "
			+ Constants.USERINFO_TABLE_NAME
			+ "(_id integer PRIMARY KEY autoincrement, type integer NOT NULL, uid varchar(30),"
			+ "username varchar(30),nickname varchar(30),email varchar(30),password varchar(30),cityid varchar(30),cityname varchar(30),gender varchar(30),birthday varchar(30),avatar_url varchar(30),status integer)";

	private static final String CREATE_SHARE_SQL = "create table "
			+ Constants.SHARE_TABLE_NAME
			+ "(_id integer PRIMARY KEY autoincrement, type integer NOT NULL, uid varchar(30),"
			+ "sina_token varchar(30),sina_expires_in varchar(30),sina_expires_time varchar(30), tencent_token varchar(30),tencent_expires_in varchar(30), "
			+ "tencent_expires_time varchar(30),tencent_clientId varchar(30), tencent_clientIp varchar(30),tencent_clientSecret varchar(30), tencent_oauthVersion varchar(30),"
			+ "tencent_openid varchar(30), tencent_openkey varchar(30), tencent_redirectUri varchar(30), tencent_scope varchar(30))";


	private static final String CREATE_CITIES_SQL = "create table "
			+ Constants.CITIES_TABLE_NAME
			+ "(_id integer PRIMARY KEY autoincrement,city_id varchar(30), name varchar(30), firstchar varchar(5), pinyin varchar(30),orders varchar(30))";


	private static final String CREATE_FAVORITE_SQL = "create table "
			+ Constants.FAVORITE_TABLE_NAME
			+ "(_id integer PRIMARY KEY autoincrement, type varchar(10), goodsid varchar(30),mallid varchar(30),favid varchar(30), timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)";
	
	private static final String CREATE_SKIMHISTORY_SQL = "create table "
			+ Constants.SKIM_HISTORY_TABLE_NAME
			+ "(_id integer PRIMARY KEY autoincrement, type integer, goodsid varchar(30), name varchar(30), cover varchar(30), saleprice varchar(30),"
			+ "mallid varchar(30),mallname varchar(30), mallCount varchar(30), timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)";
	
	private static final String DROP_SEARCHKEYHISTORY_SQL = "drop table if exists "
			+ Constants.SEARCHKEYHISTORY_TABLE_NAME;

	private static final String DROP_USER_INFO_SQL = "drop table if exists "
			+ Constants.USERINFO_TABLE_NAME;

	private static final String DROP_SHARE_SQL = "drop table if exists "
			+ Constants.SHARE_TABLE_NAME;
	
	private static final String DROP_CITIES_SQL = "drop table if exists "
			+ Constants.CITIES_TABLE_NAME;
	
	
	private static final String DROP_FAVORITE_SQL = "drop table if exists "
			+ Constants.FAVORITE_TABLE_NAME;
	
	private static final String DROP_SKIM_HISTORY_SQL = "drop table if exists "
			+ Constants.SKIM_HISTORY_TABLE_NAME;
	
	public DatabaseHelper(Context context, CursorFactory factory, int version) {
		super(context, Constants.DATABASE_NAME, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_SEARCHKEYHISTORY_SQL);
		db.execSQL(CREATE_USERINFO_SQL);
		db.execSQL(CREATE_SHARE_SQL);
		db.execSQL(CREATE_CITIES_SQL);
		db.execSQL(CREATE_FAVORITE_SQL);
		db.execSQL(CREATE_SKIMHISTORY_SQL);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		this.delete(db);
		this.onCreate(db);
	}

	public void delete(SQLiteDatabase db) {
		db.execSQL(DROP_SEARCHKEYHISTORY_SQL);
		db.execSQL(DROP_USER_INFO_SQL);
		db.execSQL(DROP_SHARE_SQL);
		db.execSQL(DROP_CITIES_SQL);
		db.execSQL(DROP_FAVORITE_SQL);
		db.execSQL(DROP_SKIM_HISTORY_SQL);
	}

}
